package com.ledim.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class CardA8PageAdapter extends PagerAdapter {

    public ArrayList<LedimChoiceCardBean> pList;
    private LayoutInflater mInflater;

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    private String groupId;
    private Activity mActivity;

    public CardA8PageAdapter(Activity activity, ArrayList<LedimChoiceCardBean> pList,
                             String groupId) {
        mInflater = LayoutInflater.from(activity);
        this.pList = pList;
        this.groupId = groupId;
        mActivity = activity;
    }

    public CardA8PageAdapter(Activity activity) {
        mInflater = LayoutInflater.from(activity);
        mActivity = activity;
    }

    public void setDatas(ArrayList<LedimChoiceCardBean> pList, String groupId) {
        this.pList = pList;
        this.groupId = groupId;
        notifyDataSetChanged();
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return pList == null ? 0 : pList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View convertView = mInflater.inflate(R.layout.card_a8_item, null);
        SimpleDraweeView mPhoto =
                (SimpleDraweeView) convertView.findViewById(R.id.card_a8_item_photo);

        final LedimChoiceCardBean card = pList.get(position);
        if (card.photo != null && !TextUtils.isEmpty(card.photo.thumb)) {
            FrescoUtil.loadUrl(card.photo.thumb, mPhoto);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LedimFunctionUtils.jumpTargetActivity(mActivity, card, groupId);
            }
        });
        view.addView(convertView, 0);
        return convertView;
    }

}
